# -*- coding:utf-8 -*-

import logging
from openerp.osv import fields, osv
from openerp import tools


__logger = logging.getLogger(__name__)

class pm_security_station(osv.osv):
    _name = "pm.security.station"
    _description = "Security Station"
    _columns = {
        "name":fields.char("Station Name", size=32, required=True),
        "remark":fields.text("Remark"),
    }
pm_security_station()

class pm_security_guard_arrange(osv.osv):
    _name = "pm.security.guard.arrange"
    _description = "security guard arrangement"
    _columns = {
                "duty_code":fields.char("Duty code",size=32,required=True,readonly=True,states={'no_inspect':[('readonly',False)]}),
                "person_on_duty":fields.many2one("res.users", "person_on_duty", ondelete="cascade", required=True,readonly=True,states={'no_inspect':[('readonly',False)]}),
                "person_in_charge":fields.many2one("res.users","Person in charge",required=True,readonly=True,states={'no_inspect':[('readonly',False)]}),
                "duty_time":fields.date("Duty time",readonly=True,states={'no_inspect':[('readonly',False)]}),
                "work_content":fields.text("Work content",readonly=True,states={'no_inspect':[('readonly',False)]}),
                "exception":fields.text("Exception",readonly=True,states={'no_inspect':[('readonly',False)]}),
                "handle_result":fields.text("Handle result",readonly=True,states={'no_inspect':[('readonly',False)]}),
                "remark":fields.text("Remark"),
                "inspector":fields.many2one("res.users","Inspector",required=True,readonly=True,states={'no_inspect':[('readonly',False)]}),
                "inspect_time":fields.date("Inspect time",readonly=True,states={'no_inspect':[('readonly',False)]}),
                "inspect_result":fields.text("Inspect result",readonly=True,states={'no_inspect':[('readonly',False)]}),
                "state":fields.selection([("no_inspect","No inspect"),("inspected","Inspected")],"State",required=True,readonly=True),
                "verify_leader":fields.many2one("res.users","Veryfy leader"),
                "station":fields.many2one("pm.security.station","Station",readonly=True,states={'no_inspect':[('readonly',False)]}),
   }
    _rec_name = "duty_code"
    _sql_constraints=[("duty_code_uniq","unique(duty_code)","duty_code must be unique")]
    _defaults ={
                'state':'no_inspect'
                }
    def leader_confirm(self,cr,uid,ids,*arg):
        self.write(cr,uid,ids,{'state':'inspected','verify_leader':uid})
        return True
    
pm_security_guard_arrange()

class pm_vehicle_manage(osv.osv):
    _name="pm.vehicle.manage"
    _description = "vehicle management"
    
    def _get_image(self, cr, uid, ids, name, args, context=None):
        result = dict.fromkeys(ids, False)
        for obj in self.browse(cr, uid, ids, context=context):
            result[obj.id] = tools.image_get_resized_images(obj.image)
        return result
    
    def _set_image(self, cr, uid, id, name, value, args, context=None):
        return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
    
    _columns = {
                "owner":fields.char("Owner",size=32,required=True),
                "plate_number":fields.char("Plate number",size=32,required=True),
                "brand":fields.char("Brand",size=64,required=True),
                "vehicle_type":fields.char("Vehicle type",size=32),
                "vehicle_color":fields.char("Vehicle color",size=32),
                "engine_code":fields.char("Engine code",size=128,required=True),
                "vehicle_frame_no":fields.char("Vehicle frame no",size=128,required=True),
                "underpan_no":fields.char("Underpan no",size=128,required=True),
                "carport1":fields.char("Carport1"),
                "carport2":fields.char("Carport2"),
                "pass_code":fields.char("Pass code",size=16,required=True),
                "driving_license_code":fields.char("Driving license code",size=64,required=True),
                "carport_fee":fields.float("Carport fee"),
                "is_paid":fields.boolean("Is paid"),
                "card_open_date":fields.date("Card open date"),
                "card_close_date":fields.date("Card close date"),
                "owner_phone":fields.char("Owner phone",size=16,required=True),
                "remark":fields.text("Remark"),
                "create_uid":fields.many2one("res.users","Create  person",required=True,readonly=True),
                "verify_person":fields.many2one("res.users","Verify person"),
                "state":fields.selection([("no_verify","No verify"),("verified","Verified")],"State",required=True,readonly=True),
                # image: all image fields are base64 encoded and PIL-supported
                'image': fields.binary(" Vehicle photo",
                    help="This field holds the image used as photo for the employee, limited to 1024x1024px."),
                'image_medium': fields.function(_get_image, fnct_inv=_set_image,
                    string="Medium-sized photo", type="binary", multi="_get_image",
                    store = {
                        'pm.vehicle.manage': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
                    },
                    help="Medium-sized photo of the employee. It is automatically "\
                         "resized as a 128x128px image, with aspect ratio preserved. "\
                         "Use this field in form views or some kanban views."),
                'image_small': fields.function(_get_image, fnct_inv=_set_image,
                    string="Smal-sized photo", type="binary", multi="_get_image",
                    store = {
                        'pm.vehicle.manage': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
                    },
                    help="Small-sized photo of the employee. It is automatically "\
                         "resized as a 64x64px image, with aspect ratio preserved. "\
                         "Use this field anywhere a small image is required."),
                }
    _sql_constraints = [
        ('plate_number_uniq', 'unique(plate_number)', 'The plate_number must be unique!'),
        ('vehicle_frame_no_uniq', 'unique(vehicle_frame_no)', 'The vehicle_frame_no must be unique!'),
        ('underpan_no_uniq', 'unique(underpan_no)', 'The underpan_no must be unique!'),
    ]
    _rec_name = "plate_number"
    _defaults={
               "state":"no_verify"
               }
    def finish_verify(self,cr,uid,ids,*arg):
        self.write(cr, uid, ids, {"state":"verified","verify_person":uid})
        return True
pm_vehicle_manage()   


class pm_visitor_register(osv.osv):
    _name="pm.visitor.register"
    _description="visitor register"
    _columns = {
                "register_code":fields.char("Register code",size=32,required=True),
                "visitor_name":fields.char("Visitor name",size=32,required=True),
                "gender":fields.selection([("male","Male"),("female","Female")],"Gender",required=True),
                "ID_no":fields.char("ID number",size=32,required=True),
                "phone_no":fields.char("Phone number",size=16),
                "purpose":fields.text("Purpose"),
                "create_uid":fields.many2one("res.users","Creator",required=True,readonly=True),
                "state":fields.selection([("registering","Registering"),("in","IN"),("left","Left")],"Status",required=True,readonly=True),
                "write_uid":fields.many2one("res.users","Writer",readonly=True),
                }
    _sql_constraints=[
                          ("register_code_uniq","unique(register_code)","register_code must be unique"),
                          ]
    _defaults={
               "state":"registering"
               }
    _rec_name = "register_code"
    def write_in(self,cr,uid,ids,*arg):
        self.write(cr,uid,ids,{"state":"in"})
        return True
    def write_left(self,cr,uid,ids,*arg):
        self.write(cr, uid,ids,{"state":"left"})
        return True
    
pm_visitor_register()


# class pm_baoan_anpai(osv.osv):
#     _name = "pm.baoan.anpai"
#     _description="保安安排"
#     _columns = {
#                 "start_date":fields.date("Start Date",required=True),
#                 "end_date":fields.date("End Date",required=True),
#                 "banci":fields.char(string = "Banci",size=64,required=True),
#                 "shiduan":fields.char(string="Shiduan",size=64,required=True),
#                 "area":fields.char(string="Area",size=128),
#                 "persons_on_duty":fields.many2many('res.users',string="Persons on duty"),
#                 "station":fields.many2one("pm.security.station",string="Station"),
#                 "remark":fields.text("Remark"),
#                 }
#     _sec_name = "banci"
#class pm_examination(osv.osv):


class pm_xf_facility(osv.osv):
    _name = "pm.xf.facility"
    _description = "Facility"
    _columns = {
                "name":fields.char(string="Facility Name",size=64,required=True),
                "model":fields.char(string="Facility Model",size=64),
                "unit":fields.char(string="Unit"),
                "quantity":fields.integer(string="Quantity"),
                "place":fields.char(string="Place"),
                "person_in_charge":fields.many2one('res.users',string="Person in charge"),
                "remark":fields.text(string="Remark"),
                }
pm_xf_facility()
    
class pm_xf_patrol(osv.osv):
    _name="pm.xf.patrol" 
    _description="Patrol"   
    _columns={
              "patrol_time":fields.date(string="Patrol time",required=True),
              "patrol_person":fields.many2many("res.users",string="Patrol person",required=True),
              "patrol_area":fields.char(string="Patrol area"),
              "patrol_result":fields.text(string="Patrol result"),
              "handle_advice":fields.text(string="Handle advice"),
              "remark":fields.text(string="Remark"),
              }
    _rec_name="patrol_time"
pm_xf_patrol()    

class pm_xf_drill(osv.osv):
    _name="pm.xf.drill"
    _description="Drill"
    _columns = {
                "name":fields.char(string="Drill name",required=True,size=64),
                "start_date":fields.date(string="Start date",required=True),
                "end_date":fields.date(string="End date",required=True),
                "actor_no":fields.integer(string="Actor no",required=True),
                "drill_purpose":fields.text(string="Drill purpose",required=True),
                "assist_org":fields.char(string="Assist organization"),
                "drill_content":fields.text(string="Drill content"),
                "drill_result":fields.text(string="Drill result"),
                "remark":fields.text(string="Remark"),
                }
pm_xf_drill()
 
class pm_xf_accident(osv.osv):
    _name="pm.xf.accident"
    _description="Accident"
    _columns = {
                "name":fields.char(string="Accident name",required=True),
                "accident_date":fields.date(string="Accident date",required=True),
                "accident_area":fields.char(string="Accident area",required=True),
                "reason":fields.text(string="Accident reason",required=True),
                "related_person":fields.many2many("res.users",string="Related person"),
                "hanlde_result":fields.text(string="Handle result"),
                "extent_of_damage":fields.text(string="Extent of damage"),
                "remark":fields.text(string="Remark"),
                }
pm_xf_accident()    
    